package com.hxd.otc.module.wallet.activity;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseRecyclerActivity;
import com.hxd.otc.dialog.ConfirmDeleteDialog;
import com.hxd.otc.module.my.bean.WalletContactsBean;
import com.hxd.otc.module.wallet.adapter.WalletContactAdapter;
import com.hxd.otc.module.wallet.contract.WalletContactContract;
import com.hxd.otc.module.wallet.presenter.WalletContactPresenter;
import com.hxd.otc.widget.SwipeItemLayout;
import com.hxd.otc.widget.TitleBar;

import butterknife.BindView;

/**
 * Created by lzy on 2018/8/6 0006.
 * description ${钱包联系人}
 */
public class WalletContactActivity extends BaseRecyclerActivity<WalletContactPresenter, WalletContactAdapter, WalletContactsBean> implements WalletContactContract.View, WalletContactAdapter.OnDeleteContactListener {

    @BindView(R.id.title_bar)
    TitleBar mTitleBar;

    public static final String EXTRA_IS_SHOW_ADD = "isShowAdd";
    public static final String EXTRA_FILTER_COIN_TYPE = "filterCoinType";
    public static final String EXTRA_FILTER_COIN_ID = "filterCoinId";
    public static final String EXTRA_OPEN_SOURCE = "address_type";

    public static final int EXTRA_OPEN_SOURCE_MY = 0;//我的
    public static final int EXTRA_OPEN_SOURCE_COIN_WITHDRAW = 1;//提币
    public static final int EXTRA_OPEN_SOURCE_AEC_QUICK_WITHDRAW = 2;//转账


    /**
     * setResult 时返回钱包地址的取值键
     */
    public static final String RESULT_EXTRA_ADDRESS = "wallet_address";

    private static final int REQUEST_CODE_ADD_CONTACT = 1001;

    private boolean mIsShowAdd;
    private String mFilterCoinType;
    private String id = "";
    private int mOpenSource;

    private ConfirmDeleteDialog mConfirmDeleteDialog;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_wallet_contact;
    }

    @Override
    protected WalletContactAdapter createAdapter() {
        return new WalletContactAdapter(this, mIsShowAdd);
    }

    @Override
    protected WalletContactPresenter createPresenter() {
        return new WalletContactPresenter(this);
    }

    @Override
    protected void initView() {
        initBundle();
        mPresenter.setId(id);
        super.initView();
        mTitleBar.getRightText().setVisibility(mIsShowAdd ? View.VISIBLE : View.GONE);
        mTitleBar.setOnTitleBarItemClickListener(new TitleBar.OnTitleBarItemClickListener() {
            @Override
            public void onBack(View v) {
            }

            @Override
            public void onRight(View v) {
                //打开添加钱包联系人
                $startActivityForResult(AddWalletContactActivity.class, REQUEST_CODE_ADD_CONTACT);
            }
        });
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mRecyclerView.addOnItemTouchListener(new SwipeItemLayout.OnSwipeItemTouchListener(this));
        mAdapter.setOnDeleteContactListener(this);
        mPresenter.setFilterCoinType(mFilterCoinType);
    }

    private void initBundle() {
        Intent intent = getIntent();
        mIsShowAdd = intent.getBooleanExtra(EXTRA_IS_SHOW_ADD, false);
        mOpenSource = intent.getIntExtra(EXTRA_OPEN_SOURCE, EXTRA_OPEN_SOURCE_MY);
        mFilterCoinType = intent.getStringExtra(EXTRA_FILTER_COIN_TYPE);
        mFilterCoinType = TextUtils.isEmpty(mFilterCoinType) ? "" : mFilterCoinType;
        if (intent.hasExtra(EXTRA_FILTER_COIN_ID)) {
            id = intent.getStringExtra(EXTRA_FILTER_COIN_ID);
        }
    }

    @Override
    public void deleteContactsSuccess(int position, WalletContactsBean walletContactsBean) {
        mAdapter.remove(position);
    }

    @Override
    public void onDeleteContact(final int position, final WalletContactsBean walletContactsBean) {
        mConfirmDeleteDialog = new ConfirmDeleteDialog(this)
                .setTitleText(R.string.wallet_confirm_the_deletion)
                .setOnConfirmClickListener(new ConfirmDeleteDialog.OnConfirmClickListener() {
                    @Override
                    public void onConfirm() {
                        mPresenter.deleteContacts(position, walletContactsBean);
                    }
                });
        mConfirmDeleteDialog.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE_ADD_CONTACT) {
            mPresenter.onRefresh();
        }
    }

    @Override
    public void onItemClick(int position) {
        if (mOpenSource != EXTRA_OPEN_SOURCE_MY) {
            Intent intent = new Intent();
            intent.putExtra(RESULT_EXTRA_ADDRESS, mAdapter.getItem(position).address);
            setResult(Activity.RESULT_OK, intent);
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mConfirmDeleteDialog != null) {
            mConfirmDeleteDialog.dismiss();
            mConfirmDeleteDialog = null;
        }
    }
}
